package com.koron.housing.apply.service.impl;


import com.koron.housing.apply.bean.po.HousingStaffRank;
import com.koron.housing.apply.mapper.HousingStaffRankMapper;
import com.koron.housing.apply.service.HousingStaffRankService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 职称信息(HousingStaffRank)表服务实现类
 *
 * @author jzp
 * @since 2022-02-26 15:22:41
 */
@Service("rankService")
public class HousingStaffRankServiceImpl implements HousingStaffRankService {

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public HousingStaffRank getById(SessionFactory factory, String id) {
        HousingStaffRankMapper mapper = factory.getMapper(HousingStaffRankMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<HousingStaffRank> list(SessionFactory factory, HousingStaffRank housingStaffRank) {
        HousingStaffRankMapper mapper = factory.getMapper(HousingStaffRankMapper.class);
        return mapper.selectList(housingStaffRank);
    }

    @Override
    @TaskAnnotation("listByApplyId")
    public List<HousingStaffRank> listByApplyId(SessionFactory factory, String applyId) {
        HousingStaffRank housingStaffRank = new HousingStaffRank();
        housingStaffRank.setApplyId(applyId);
        return list(factory, housingStaffRank);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, HousingStaffRank housingStaffRank) {
        HousingStaffRankMapper mapper = factory.getMapper(HousingStaffRankMapper.class);
        return mapper.insertOrUpdate(housingStaffRank) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<HousingStaffRank> rankList) {
        if (CollectionUtils.isEmpty(rankList)) {
            return false;
        }
        HousingStaffRankMapper mapper = factory.getMapper(HousingStaffRankMapper.class);
        return mapper.insertOrUpdateBatch(rankList) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, HousingStaffRank housingStaffRank) {
        HousingStaffRankMapper mapper = factory.getMapper(HousingStaffRankMapper.class);
        return mapper.update(housingStaffRank) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        HousingStaffRankMapper mapper = factory.getMapper(HousingStaffRankMapper.class);
        return mapper.hidden(id) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteBatchIds")
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        HousingStaffRankMapper mapper = factory.getMapper(HousingStaffRankMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @TaskAnnotation("removeByApplyId")
    public boolean removeByApplyId(SessionFactory factory, String applyId) {
        if (StringUtils.isEmpty(applyId)) {
            return false;
        }
        List<HousingStaffRank> rankList = listByApplyId(factory, applyId);
        return deleteBatchIds(factory, rankList.stream().map(HousingStaffRank::getId).collect(Collectors.toList()));

    }
}
